<template>
  <div class="app-container">
    <!--搜索栏-->
    <el-row :gutter="15" class="mb8">
      <el-col :span="1.5">
        <el-button icon="el-icon-plus" plain size="mini" @click="handleAdd">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button :disabled="single" icon="el-icon-edit" plain size="mini" @click="handleEdit">修改
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button :disabled="multiple" icon="el-icon-delete" plain size="mini" @click="handleDelete">删除
        </el-button>
      </el-col>
      <el-form ref="queryParamsRef" :model="queryParams" class="el-form-search" inline label-width="80px"
        @submit.prevent>
        <!--设备名称-->
        <el-form-item label="设备名称" prop="devName">
          <el-input v-model.trim="queryParams.devName" class="el-form-search-item" clearable
            placeholder="设备名称"></el-input>
        </el-form-item>

        <!-- 下拉框 -->
        <el-form-item class="el-form-search-item" label="变电站" prop="substationNum">
          <el-select v-model="queryParams.substationNum" clearable placeholder="变电站">
            <el-option v-for="item in substationOptions" :key="item.num" :label="item.substationName"
              :value="item.num"></el-option>
          </el-select>
        </el-form-item>

        <!-- 设备状态 -->
        <el-form-item class="el-form-search-item" label="设备状态" prop="devStatus">
          <el-select v-model="queryParams.devStatus" clearable placeholder="设备状态">
            <el-option v-for="dict in devStatusOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item class="el-form-search-item">
          <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索
          </el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="handleRefresh">重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-row>

    <!--展示列表-->
    <el-table :data="deviceList" :height="tableHeight"
      v-loading="loading" border highlight-current-row stripe
      @selection-change="handleSelectChange">
      <el-table-column align="center" type="selection" width="55" />
      <el-table-column align="center" label="#" type="index" width="50">
        <template slot-scope="scope">
          <span>{{
            (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1
          }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="设备名称" prop="devName"></el-table-column>
      <el-table-column align="center" label="设备编号" prop="devNum"></el-table-column>
      <!-- <el-table-column
        align="center"
        label="客户"
        prop="customer.companyName"
      ></el-table-column> -->
      <el-table-column align="center" label="所属变电站" show-overflow-tooltip
        prop="substation.substationName"></el-table-column>
      <el-table-column align="center" label="设备状态" prop="deviceStatus.name">
        <template slot-scope="scope">
          <el-tag :type="statusFormat(scope.row)">{{ scope.row.deviceStatus.name }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="设备型号" prop="devModel.name"></el-table-column>
      <el-table-column align="center" label="设备描述" prop="devDesc"></el-table-column>
      <el-table-column align="center" label="设备厂商" prop="devManufacturerName"></el-table-column>
      <el-table-column align="center" label="出厂日期" prop="devProductionDate"></el-table-column>
      <el-table-column align="center" label="sim卡" prop="simCard.phone"></el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="170">
        <template slot-scope="scope">
          <el-button icon="el-icon-view" size="mini" type="text" @click="handleView(scope.row)">查看
          </el-button>
          <el-button icon="el-icon-edit" size="mini" type="text" @click="handleEdit(scope.row)">修改
          </el-button>
          <el-button icon="el-icon-delete" size="mini" type="text" @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="800px">
      <el-form ref="formDataRef" :model="formData" :rules="rule" label-width="100px">
        <!--第一行-->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="设备名称" prop="devName">
              <el-input v-model="formData.devName" placeholder="设备名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="devNum">
              <el-input v-model="formData.devNum" placeholder="设备编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <!--第二行-->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="设备型号" prop="devModelNum">
              <el-select v-model="formData.devModelNum" class="from-select" clearable filterable placeholder="请选择设备型号">
                <el-option v-for="item in devModelOptions" :key="item.num" :label="item.name"
                  :value="item.num"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="变电站" prop="substationNum">
              <el-select v-model="formData.substationNum" class="from-select" clearable filterable placeholder="请选择变电站">
                <el-option v-for="item in substationOptions" :key="item.num" :label="item.substationName"
                  :value="item.num"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!--第三行-->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="设备厂商" prop="devManufacturerName">
              <el-input v-model="formData.devManufacturerName" placeholder="设备厂商" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出厂日期" prop="devProductionDate">
              <el-date-picker v-model="formData.devProductionDate" placeholder="出厂日期" type="date">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <!--第四行-->
        <el-row :gutter="15">
          <el-col :span="12">
            <el-form-item label="维修日期" prop="maintainDate">
              <el-date-picker v-model="formData.maintainDate" placeholder="维修日期" type="date">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下次维修日期" prop="nextMaintainDate">
              <el-date-picker v-model="formData.nextMaintainDate" placeholder="下次维修日期" type="date">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <!--第五行-->
        <el-row :gutter="15">
          <!-- <el-col :span="12">
            <el-form-item label="客户" prop="customerNum">
              <el-select
                clearable
                filterable
                placeholder="请选择客户"
                v-model="formData.customerNum"
                class="from-select"
              >
                <el-option
                  v-for="item in customerOptions"
                  :key="item.num"
                  :value="item.num"
                  :label="item.companyName"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="sim卡" prop="simCardNum">
              <el-select v-model="formData.simCardNum" class="from-select" clearable filterable placeholder="请选择sim卡">
                <el-option v-for="item in simCardOptions" :key="item.num" :label="item.phone"
                  :value="item.num"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- 第六行 -->
        <el-row :gutter="15">
          <el-col :span="24">
            <!--备注-->
            <el-form-item label="设备描述" prop="devDesc">
              <el-input v-model.trim="formData.devDesc" :maxlength="100" :rows="2" clearable placeholder="设备描述"
                resize="none" show-word-limit type="textarea"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormData">提交 </el-button>
        <el-button @click="close">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDeviceList,
  addDevice,
  getByNum,
  editDevice,
  removeBatch,
} from "@/api/device/device";
import { listSubstation } from "@/api/maintenance/substation";
import { listDevModel } from "@/api/device/devModel";
import { listCustomer } from "@/api/maintenance/customer";
import { listSimCard } from "@/api/maintenance/simCard";

export default {
  data() {
    return {
      // 表格信息
      tableHeight: "calc(100vh - 200px)",
      loading: true, // 遮罩层
      total: 0, //数据总条数
      deviceList: [],
      ids: [],
      single: true,
      multiple: true,

      // 搜索框
      queryParams: {
        pageNum: 1,
        pageSize: 30,
        // orderByColumn: "update_time",
        // isAsc: "ASC",
      },

      //对话框数据
      title: "",
      open: false,
      formData: {},
      substationOptions: [],
      devModelOptions: [],
      customerOptions: [],
      simCardOptions: [],
      devStatusOptions: [],
      rule: {
        devName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" },
        ],
        devNum: [
          { required: true, message: "设备编号不能为空", trigger: "blur" },
        ],
        devModelNum: [
          { required: true, message: "设备型号不能为空", trigger: "blur" },
        ],
        substationNum: [
          {
            required: true,
            message: "变电站不能为空",
            trigger: "blur",
          },
        ],
        customerNum: [
          {
            required: true,
            message: "客户不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    statusFormat() {
      return (row, column) => {
        switch (row.deviceStatus) {
          case "0":
            return "success";
          case "1":
            return "warning";
          case "2":
            return "danger";
          default:
            return "info";
        }
      }
    }
  },
  created() {
    this.getList();
    //获取变电站列表
    listSubstation().then((response) => {
      this.substationOptions = response.rows;
    });
    // 获取设备类型
    listDevModel().then((response) => {
      this.devModelOptions = response.rows;
    });
    // 查询客户
    listCustomer().then((response) => {
      this.customerOptions = response.rows;
    });
    listSimCard().then((response) => {
      this.simCardOptions = response.rows;
    });
    // 设备状态列表
    this.getDicts("dev_status").then((response) => {
      this.devStatusOptions = response.data;
    });
  },
  methods: {
    //查询设备信息
    getList() {
      this.loading = true;
      getDeviceList(this.queryParams).then((res) => {
        this.deviceList = res.rows;
        this.total = res.total;
          this.$refs.elTable.doLayout();
        this.loading = false;
      });
    },
    // 新增设备
    handleAdd() {
      this.resetForm("formDataRef");
      this.formData = {};
      this.title = "新增设备";
      this.open = true;
    },
    // 编辑设备
    handleEdit(row) {
      this.resetForm("formDataRef");
      this.title = "修改设备";
      this.open = true;
      const num = row.num || this.ids;
      // 根据num获取formData的数据，为了修改；
      getByNum(num).then((response) => {
        this.formData = response.data;
      });
    },
    // 删除设备
    handleDelete(row) {
      const nums = row.num || this.ids;
      this.$modal
        .confirm("确认删除吗？")
        .then(function () {
          return removeBatch(nums);
        })
        .then(() => {
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .catch(() => { });
    },

    // 查看设备明细
    handleView(row) {
      this.$router.push({
        path: "/device/device-edit",
        name: "DeviceEdit",
        query: { deviceId: row.num,title: row.devName },
      });
    },
    handleSelectChange(val) {
      this.ids = val.map((item) => item.num);
      this.single = this.ids.length !== 1;
      this.multiple = !this.ids.length;
    },
    submitFormData() {
      this.$refs["formDataRef"].validate((valid) => {
        if (valid) {
          if (this.formData.num !== undefined) {
            editDevice(this.formData).then((response) => {
              this.open = false;
              this.$modal.msgSuccess("修改成功");
              this.getList();
            });
          } else {
            addDevice(this.formData).then((response) => {
              this.open = false;
              this.$modal.msgSuccess("新增成功");
              this.getList();
            });
          }
        }
      });
    },
    close() {
      this.resetForm("formDataRef");
      this.open = false;
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleRefresh() {
      console.log("重置按钮!");

      this.resetForm("queryParamsRef");
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped></style>
